/**
 * @file {偏贴 目标库panel表model}
 */
'use strict'

const {
    Model
} = require('../../../azeroth').sequelize;
const {
    panelInitData
} = require('../common/init_data');
class PanelPiantie extends Model {}

/**
 * @desc 映射需要的字段 【源库的 部分字段 --> 目标库字段】
 */
const initPanelData = (sequelize_aim) => {
    PanelPiantie.init(
        ...panelInitData, {
            freezeTableName: true,
            sequelize: sequelize_aim,
            modelName: 'panel',
            timestamps: false
        }
    )
}

/**
 * 获取圆表最新panel
 * @param {*} sequelize_source
 * @returns
 */
PanelPiantie.getLatestPanelFromSource = async (sequelize_source) => {
    let sql = 'select max(uid) as uid, panel_id from panel_info';
    let res = await sequelize_source.query(sql, {
        type: sequelize_source.QueryTypes.SELECT
    });
    return res[0].panel_id;
}
/**
 * 获取目标panle的最新id
 * @param {*} sequelize_aim
 * @returns
 */
PanelPiantie.getLatestPanelId = async (sequelize_aim) => {
    let sql = 'select max(uid) as uid from panel';
    let res = await sequelize_aim.query(sql, {
        type: sequelize_aim.QueryTypes.SELECT
    });
    return res[0].uid;
}
/**
 * 源库查询最新一条的 panel的信息
 * @param {sequelize} sequelize_source
 * @returns
 */
PanelPiantie.findLatestPanelInfo = async (sequelize_source, queryStr) => {

    let query = typeof (queryStr) == 'string' ? `b.panel_id = "${queryStr}"` : `a.uid = ${queryStr}`;
    let sql = 'select a.uid, a.jobId as job_id, b.panel_id, a.flawCount as flaw_count, a.exConfig1, a.startTime as gen_time, a.detectParam, a.comment as save_path ' +
        'from doff a inner join panel_info b on a.uid = b.doff_id where ' + query;
    let res = await sequelize_source.query(sql, {
        type: sequelize_source.QueryTypes.SELECT
    });
    return res[0];
}
/**
 * 上传panel
 * @param {Object} data 上传的panel信息对象
 * @returns
 */
PanelPiantie.uploadPolInfo = async (data) => {
    let res = await PanelPiantie.bulkCreate([data], {
        updateOnDuplicate: ['flaw_count', 'is_ok', 'length_md', 'length_cd', 'save_path', 'gen_time', 'gen_time_str', 'detectParam', 'checked']
    });
    return res;
}
module.exports = {
    PanelPiantie,
    initPanelData
};